//
//  DetailTowTableViewController.m
//  bh
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "DetailTowTableViewController.h"
#import "BrowseModel.h"
#import "PhotosTableViewCell.h"
#import "Photos.h"
#import "BrowseCollectionView.h"
#import "FirstDatailTableViewCell.h"
#import "CollectionViewCell.h"
#import "BrowseDetailModel.h"
@interface DetailTowTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *photosArray;
@property (nonatomic, strong) BrowseCollectionView *cellectionView;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) NSMutableArray *goodArray;
@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;


@end

@implementation DetailTowTableViewController
- (NSMutableArray *)photosArray {
    if (!_photosArray) {
        _photosArray = [[NSMutableArray alloc]init];
    }
    return _photosArray;
}
- (NSMutableArray *)goodArray {
    if (!_goodArray) {
        _goodArray = [[NSMutableArray alloc]init];
    }
    return _goodArray;
}

- (void)setData {
    
    NSString *stringUrl = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo",_browseModel.ID];
    NSLog(@"%@",stringUrl);
    NSLog(@"%@",_browseModel.ID);
    [LORequestManger GET:stringUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *data = dic[@"data"];
        NSDictionary *collection_liked_users = dic[@"collection_liked_users"];
        NSArray *users = collection_liked_users[@"users"];
        for (NSDictionary *usersDic in users) {
            BrowseDetailModel *browseModel = [[BrowseDetailModel alloc]init];
            [browseModel setValuesForKeysWithDictionary:usersDic];
            [_goodArray addObject:browseModel];
        }
        NSDictionary *collection_photos2 = data[@"collection_photos2"];
        NSArray *photos1 = collection_photos2[@"photos"];
        NSDictionary *dict = photos1[0];
        NSArray *value = dict[@"value"];
        for (NSDictionary *photo in value) {
            Photos *photos = [Photos shareWithDictionary:photo];
            [self.photosArray addObject:photos];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    
    self.navigationController.navigationBar.alpha = 0;
    
    // 设置表头
    // 背景图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, kHeight / 5 * 2))];
    imageView.userInteractionEnabled = YES;
    [imageView setImageWithURL:[NSURL URLWithString:_browseModel.cover_pic]];
    
    // 个人图像
    UIImageView *personalPictureView = [[UIImageView alloc]initWithFrame:(CGRectMake(kWidth / 2 - 40, kHeight / 10 - 20, 80, 80))];
    personalPictureView.userInteractionEnabled = YES;
    [personalPictureView setImageWithURL:[NSURL URLWithString:_browseModel.author_face]];
    personalPictureView.layer.masksToBounds = YES;
    personalPictureView.layer.borderWidth = 2;
    personalPictureView.layer.cornerRadius = personalPictureView.bounds.size.height / 2;
    personalPictureView.layer.borderColor = [[UIColor whiteColor]CGColor];
    [imageView addSubview:personalPictureView];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:(CGRectMake(0, kHeight / 10 + 70, kWidth, 20))];
    nameLabel.text = _browseModel.title;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont boldSystemFontOfSize:18];
    [imageView addSubview:nameLabel];
    
    UILabel *addLabel = [[UILabel alloc]initWithFrame:(CGRectMake(0, kHeight / 10 + 90, kWidth, 30))];
    addLabel.text = _browseModel.title_sub;
    addLabel.textAlignment = NSTextAlignmentCenter;
    addLabel.textColor = [UIColor whiteColor];
    addLabel.font = [UIFont systemFontOfSize:22];
    [imageView addSubview:addLabel];
    
    _seg = [[UISegmentedControl alloc]initWithItems:@[@"图片",@"日记",@"清单"]];
    _seg.frame = CGRectMake(0, kHeight / 5 * 2 - 40, kWidth, 40);
    _seg.tintColor = [UIColor orangeColor];
    _seg.backgroundColor = [UIColor whiteColor];
    [_seg addTarget:self action:@selector(changeValueAction:)forControlEvents:(UIControlEventValueChanged)];
    [imageView addSubview:_seg];
    
    self.tableView.tableHeaderView = imageView;
    
    // 注册单元格
    [self.tableView registerClass:[FirstDatailTableViewCell class] forCellReuseIdentifier:@"cell1"];
    
    [self.tableView registerClass:[BrowseCollectionView class] forCellReuseIdentifier:@"cell2"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell3"];
    
    
    // 返回button
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(10, 10, 40, 40);
    [button setImage:[UIImage imageNamed:@"arrowdown.png"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(goBack1) forControlEvents:(UIControlEventTouchUpInside)];
    [imageView addSubview:button];
    
   
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}




- (void)goBack1 {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)changeValueAction:(UIButton *)a{
    if (_seg.selectedSegmentIndex == 0) {
        [self.tableView reloadData];
       NSLog(@"1234");
    }else if (_seg.selectedSegmentIndex == 1){
        NSLog(@"1235");
        
    } else {
        NSLog(@"1236");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
// 设置区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
    
    UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 40))];
  
    
    _button1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _button1.frame = CGRectMake(kWidth / 2 - 80, 10, 80, 20);
    [_button1 setTitle:@"所有照片" forState:(UIControlStateNormal)];
    [_button1 addTarget:self action:@selector(allPhotos:) forControlEvents:(UIControlEventTouchUpInside)];
    _button1.tag = 100;
   [_button1 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [view addSubview:_button1];
    
    _button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _button2.frame = CGRectMake(kWidth / 2 + 10, 10, 80, 20);
    _button2.tag = 101;
    [_button2 setTitle:@"完工照片" forState:(UIControlStateNormal)];
    [_button2 addTarget:self action:@selector(downPhotos:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_button2 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [view addSubview:_button2];
    
    return view;
    } else if(section == 1 ) {
        UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 40))];
        UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 130, 20))];
        label.text = @"赞过的人";
        label.textColor = [UIColor grayColor];
        [view addSubview:label];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(kWidth - 40, 10, 30, 20);
        [button setTitle:@"更多" forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [view addSubview:button];
        
       
        return view;
    } else {
         UIView *view = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 40))];
        UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(10, 10, 80, 20))];
        label.text = @"评论";
        label.textColor = [UIColor grayColor];
        [view addSubview:label];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(kWidth - 40, 10, 30, 20);
        [button setTitle:@"更多" forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [view addSubview:button];
       
        return view;
    }
    
}
// 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   
    return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//   FirstDatailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        FirstDatailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.tag = 101;
        [cell.FirstCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];
        cell.FirstCollectionView.delegate =self;
        cell.FirstCollectionView.dataSource =self;
        [cell.FirstCollectionView reloadData];
       return cell;
    } else if (indexPath.section == 1){
        BrowseCollectionView *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" ];
        
        cell.model = _goodArray[indexPath.row];
        if (_goodArray.count == 0) {
            return cell;
        }
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.imageView.image = [UIImage imageNamed:@""];
        UITextField *field = [[UITextField alloc] initWithFrame:(CGRectMake(40, 10, kWidth - 80, 20))];
        field.text = @"请输入评论";
    
        [cell.textLabel addSubview:field];
        return cell;
    }
    // Configure the cell...
    
    return nil;
}

//collectionView 的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 1) {
        return self.goodArray.count;
    }
    return self.photosArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
    cell.picture = self.photosArray[indexPath.item];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 1000 ;
    }else if (indexPath.section == 1){
        if (_goodArray.count == 0) {
            return 0;
        }
        return 40;
    }else {
        return 40;
    }
    
}

// button1点击方法
- (void)allPhotos:(UIButton *)button {
    [_button1 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [_button2 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    
}

// button1点击方法
- (void)downPhotos:(UIButton *)button {
    [_button1 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [_button2 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
